package com.xgure.service.impl;

import com.xgure.constant.JwtConstant;
import com.xgure.constant.MessageConstant;
import com.xgure.dto.EmployeeLoginDTO;
import com.xgure.dto.EmployeeRegisterDTO;
import com.xgure.dto.EmployeeUpdateDTO;
import com.xgure.excption.ValidLoginException;
import com.xgure.mapper.EmployeeMapper;
import com.xgure.pojo.Employee;
import com.xgure.properties.JwtProperties;
import com.xgure.service.EmployeeService;
import com.xgure.utils.BaseContextUtil;
import com.xgure.utils.JwtUtil;
import com.xgure.vo.EmployeeLoginVO;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    @Resource
    private JwtProperties jwtProperties;

    /**
     * 员工登录
     */
    @Override
    public EmployeeLoginVO login(EmployeeLoginDTO employeeDTO) {
        // 判断当前用户名，手机号，以及邮箱是否至少有一个不为空
        if (employeeDTO.getUsername() == null && employeeDTO.getPhone() == null && employeeDTO.getEmail() == null) {
            // 抛出异常
            throw new ValidLoginException(MessageConstant.REQUIRED_VALID_INPUT);
        }

        // 判断密码是否为空
        if (employeeDTO.getPassword() == null) {
            // 抛出异常
            throw new ValidLoginException(MessageConstant.NEED_PASSWORD);
        }

        // 封装查询信息
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
        // 登录SQL
        List<Employee> results = employeeMapper.getEmployeeByInfo(employee);
        // 判断是否查询到对应数据
        if (results == null || results.isEmpty()) {
            // 登录失败
            throw new ValidLoginException(MessageConstant.USERNAME_OR_PASSWORD_INCORRECT);
        }
        Employee resultEmployee = results.get(0);
        // 登录成功

        // 记录当前登录的shopID和用户ID
        if (resultEmployee.getShopId() != null) {
            BaseContextUtil.setCurrentShopId(resultEmployee.getShopId());
        }
        BaseContextUtil.setCurrentUserId(resultEmployee.getId());

        // 生成jwt令牌
        Map<String, String> claims = new HashMap<>();
        claims.put(JwtConstant.CLAIM, resultEmployee.getId().toString());
        String token = JwtUtil.generateToken(jwtProperties.getSecret(), claims);

        // 封装返回值
        EmployeeLoginVO employeeLoginVO = new EmployeeLoginVO();
        BeanUtils.copyProperties(resultEmployee, employeeLoginVO);
        employeeLoginVO.setToken(token);
        return employeeLoginVO;

    }

    /**
     * 员工注册
     */
    @Override
    public void register(EmployeeRegisterDTO employeeRegisterDTO) {
        // 判断当前信息齐全，不为空
        if (employeeRegisterDTO.getUsername() == null || employeeRegisterDTO.getUsername().trim().isEmpty() ||
                employeeRegisterDTO.getPhone() == null || employeeRegisterDTO.getPhone().trim().isEmpty() ||
                employeeRegisterDTO.getEmail() == null || employeeRegisterDTO.getEmail().trim().isEmpty() ||
                employeeRegisterDTO.getPassword() == null || employeeRegisterDTO.getPassword().trim().isEmpty() ||
                employeeRegisterDTO.getFirstName() == null || employeeRegisterDTO.getFirstName().trim().isEmpty() ||
                employeeRegisterDTO.getLastName() == null || employeeRegisterDTO.getLastName().trim().isEmpty() ||
                employeeRegisterDTO.getIdNumber() == null || employeeRegisterDTO.getIdNumber().trim().isEmpty()
        ) {
            // 抛出异常
            throw new ValidLoginException(MessageConstant.NEED_MORE_PERSONAL_INFORMATION);
        }
        // 封装判断各信息是否被占用
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeRegisterDTO, employee);
        // 判断当前用户名、手机号、邮箱是否已被占用
        List<Employee> employees = employeeMapper.validRegister(employee);
        if (employees != null && !employees.isEmpty()) {
            // 抛出异常
            employees.forEach((e) -> {
                if (e.getUsername() != null && e.getUsername().equals(employee.getUsername())) {
                    throw new ValidLoginException(MessageConstant.USERNAME_IS_OCCUPIED);
                } else if (e.getPhone() != null && e.getPhone().equals(employee.getPhone())) {
                    throw new ValidLoginException(MessageConstant.PHONE_IS_OCCUPIED);
                } else if (e.getEmail() != null && e.getEmail().equals(employee.getEmail())) {
                    throw new ValidLoginException(MessageConstant.EMAIL_IS_OCCUPIED);
                }
            });
        } else {
            // 注册SQL
            employeeMapper.register(employee);
        }
    }

    /**
     * 员工信息更新
     */
    @Override
    public Employee update(EmployeeUpdateDTO employeeUpdateDTO) {
        // 封装数据
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeUpdateDTO, employee);
        // 更新SQL
        employeeMapper.update(employee);
        List<Employee> employees = employeeMapper.getEmployeeByInfo(employee);
        return employees.get(0);
    }

    /**
     * 根据员工姓名查询员工
     *
     * @return 员工信息
     */
    @Override
    public Employee getEmployeeByName(String name) {
        return employeeMapper.getEmployeeByName(name);
    }
}
